package com.hlt.controller.admin;


import com.hlt.dto.CategoriesPageQueryDTO;
import com.hlt.entity.Categories;
import com.hlt.result.PageResult;
import com.hlt.result.Result;
import com.hlt.service.ICategoriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzf
 * @since 2024-11-25
 */
@RestController
@Controller("adminCategoriesController")
@RequestMapping("/categories")
@Slf4j
@Api(tags = "分类接口")
public class CategoriesController {

    @Autowired
    private ICategoriesService categoriesService;

    /**
     * 新增分类
     */
    @PostMapping("/save")
    @ApiOperation("新增分类")
    public Result save(@RequestBody Categories categories) {
        log.info("新增分类：{}",categories);
        categoriesService.save(categories);
        return Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{categoryId}")
    @ApiOperation("删除分类")
    public Result delete(@PathVariable Long categoryId) {
        log.info("删除分类：{}",categoryId);
        categoriesService.removeById(categoryId);
        return Result.success();
    }

    /**
     * 根据id查询分类
     */
    @GetMapping("/{categoryId}")
    @ApiOperation("根据id查询分类")
    public Result get(@PathVariable Long categoryId) {
        log.info("根据id查询分类：{}",categoryId);
        Categories categories = categoriesService.getById(categoryId);
        return Result.success(categories);
    }

    /**
     * 修改分类
     */
    @PutMapping("/update")
    @ApiOperation("修改分类")
    public Result update(@RequestBody Categories categories) {
        log.info("修改分类：{}",categories);
        categoriesService.updateById(categories);
        return Result.success();
    }
    /**
     * 分页查询分类
     */
    @PostMapping("/page")
    @ApiOperation("分页查询分类")
    public Result page(@RequestBody CategoriesPageQueryDTO categoriesPageQueryDTO) {
        log.info("分页查询分类：{}",categoriesPageQueryDTO);
        PageResult pageResult = categoriesService.pageQuery(categoriesPageQueryDTO);
        return Result.success(pageResult);
    }

}
